package com.study.curvesample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.study.curvesample.curve.CurveView;

public class MainActivity extends AppCompatActivity {

    private PointsGetter getter;
    private String[] mathematicals;
    private CurveView cv_curve;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cv_curve = findViewById(R.id.cv_curve);

        iniData();
    }

    private void iniData() {
        getter = new PointsGetter();
        //Y = pow(2, -10 * x) * sin((x - factor / 4) * (2 * PI) / factor) + 1
        mathematicals = new String[]{"Y=X", "Y=X²", "Y=sin(X)", "Y=cos(X)", "Y=tan(X)", "Y=cot(X)",
                "波动曲线", "心形方程"};
        //默认展示Y=X
        setTitle(mathematicals[0]);
        cv_curve.setPoints(getter.getPoints(mathematicals[0]), 80f, 80f, "X:-20~20  Y:-20~20");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_01:
                cv_curve.setPoints(getter.getPoints((String) item.getTitle()), 80f, 80f, "X:-40~40  Y:-40~40");
                break;
            case R.id.menu_02:
                cv_curve.setPoints(getter.getPoints((String) item.getTitle()), 80f, 3200f, "X:-40~40  Y:-1600~1600");
                break;
            case R.id.menu_03:
            case R.id.menu_04:
                cv_curve.setPoints(getter.getPoints((String) item.getTitle()), 720f, 10f, "X:-360°~360°  Y:-5~5");
                break;
            case R.id.menu_05:
            case R.id.menu_06:
                cv_curve.setPoints(getter.getPoints((String) item.getTitle()), 360, 20f, "X:-180°~180°  Y:-10~10");
                break;
            case R.id.menu_07:
                cv_curve.setPoints(getter.getPoints((String) item.getTitle()), 2f, 4f, "X:-1~1  Y:-2~2");
                break;
            case R.id.menu_08:
                cv_curve.setPoints(getter.getPoints((String) item.getTitle()), 4f, 4f, "X:-2~2  Y:-2~2", true);
                break;
        }
        return true;
    }
}